//
//  TTHttpRequest.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/6/3.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "TTUserModel.h"
#import "AFNetworking.h"
#import "TTHttpRequest.h"
#import "TTUDIDManager.h"
//#import "LoginViewController.h"

@implementation TTHttpRequest

- (void)getStart {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer.timeoutInterval = 10.0;
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];

    [manager.requestSerializer setValue:[TTUserModel sharedObject].token forHTTPHeaderField:@"token"];
    [manager.requestSerializer setValue:@"quanjing" forHTTPHeaderField:@"companyKey"];
    [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"x-platform"];
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@:%@",APP_VERSION,APP_BUILD]  forHTTPHeaderField:@"x-version"];
    [manager.requestSerializer setValue:[TTUDIDManager getUniqueDeviceID] forHTTPHeaderField:@"DeviceNumber"];
    
    [manager GET:self.url parameters:self.getData progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *jsonDict = [Utilities dictionaryWithJsonString:responseObject];
        self.finishBlock(jsonDict);
        
        if ([jsonDict isKindOfClass:[NSDictionary class]]) {
            NSInteger code = [[jsonDict valueForKey:@"code"] integerValue];
            if (code == 401) {
                [self goLoginWithMessage:[jsonDict valueForKey:@"message"]];
            }
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        self.failedBlock(error);
    }];
    
}

- (void)postStart {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer.timeoutInterval = 10.0;
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    

    [manager.requestSerializer setValue:[TTUserModel sharedObject].token forHTTPHeaderField:@"token"];    
    [manager.requestSerializer setValue:@"quanjing" forHTTPHeaderField:@"companyKey"];
    [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"x-platform"];
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@:%@",APP_VERSION,APP_BUILD]  forHTTPHeaderField:@"x-version"];

    [manager.requestSerializer setValue:[TTUDIDManager getUniqueDeviceID] forHTTPHeaderField:@"DeviceNumber"];
    
    [manager POST:self.url parameters:self.postData progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *jsonDict = [Utilities dictionaryWithJsonString:responseObject];
        self.finishBlock(jsonDict);
        
        if ([jsonDict isKindOfClass:[NSDictionary class]]) {
            
            NSInteger code = [[jsonDict valueForKey:@"code"] integerValue];
            if (code == 401) {
                [self goLoginWithMessage:[jsonDict valueForKey:@"message"]];
            }
        }
        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        self.failedBlock(error);
    }];
    
}

- (void)goLoginWithMessage:(NSString *)msg {
    
    [[TTUserModel sharedObject] initialization]; //userModel清空当前用户的所有信息
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        UIViewController *viewController = [Utilities topViewController];
//        if (![viewController isKindOfClass:[LoginViewController class]]) {
//            LoginViewController* login = [[LoginViewController alloc] init];
//            login.showType = TT_LoginShowType_Present;
//            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
//            nav.navigationBar.translucent = NO;
//            UIViewController *spVc = viewController.navigationController? viewController.navigationController: viewController;
//            nav.modalPresentationStyle = UIModalPresentationFullScreen;
//            [spVc presentViewController:nav animated:YES completion:nil];
//        }
    });
}

@end
